package system

import (
	"gin-admin/common/dto/response"
	"gin-admin/service/dto/request"
	responseData "gin-admin/service/dto/response"
	"gin-admin/service/v1/system"

	"github.com/gin-gonic/gin"
)

type CaptchaHandler struct {
	CaptchaService system.CaptchaService
}

var CaptchaHandlerApp = &CaptchaHandler{
	CaptchaService: *system.CaptchaServiceApp,
}

// @Summary 获取验证码
// @Description 获取验证码接口
// @Tags /v1/验证码管理
// @Product json
// @Router /api/v1/common/captcha [get]
func (cc *CaptchaHandler) GetCaptchaHandler(ctx *gin.Context) {
	id, b64s, err := cc.CaptchaService.GetCaptcha()
	if err != nil {
		response.Failed(ctx)
		return
	}
	responseData := &responseData.CaptchaResponse{
		Id:          id,
		CaptchaData: b64s,
	}
	response.Success(ctx, responseData)
}

// @Summary 验证验证码
// @Description 验证验证码接口
// @Tags /v1/验证码管理
// @Product json
// @Param data body request.VerifyCaptchaRequest true "验证码"
// @Router /api/v1/common/verifyCaptcha [post]
func (cc *CaptchaHandler) VerifyCaptchaHandler(ctx *gin.Context) {
	var captchaRequest request.VerifyCaptchaRequest
	if err := ctx.ShouldBindJSON(&captchaRequest); err != nil {
		response.Failed(ctx)
		return
	}
	result, err := cc.CaptchaService.VerifyCaptcha(captchaRequest.Id, captchaRequest.Answer)
	if err != nil {
		response.FailedWithCode(ctx, response.CaptchaInvalidCode)
		return
	}

	response.Success(ctx, result)
}
